<template>
    <div class="conment">
        <!-- <h3>登录页</h3> -->
        <el-form :model="ruleForm" status-icon :rules="rules" ref="ruleFormRef" label-width="100px" class="demo-ruleForm">
        
        <el-form-item label="账号" prop="username">
            <el-input v-model.number="ruleForm.username" clearable></el-input>
        </el-form-item>

        <el-form-item label="密码" prop="password">
            <el-input type="password" v-model="ruleForm.password" autocomplete="off" clearable></el-input>
        </el-form-item>
        <el-form-item label="确认密码" prop="checkPass">
            <el-input type="password" v-model="ruleForm.checkPass" autocomplete="off" clearable></el-input>
        </el-form-item>
        
        <el-form-item>
            <el-button type="primary" @click="submitForm('ruleForm')">提交</el-button>
            <el-button @click="resetForm('ruleForm')">重置</el-button>
        </el-form-item>
        </el-form>
    </div>
</template>

<script>
// import { set } from 'vue/types/umd';
import {login} from '../api/user'
    export default {

    data() {
    //   var checkAge = (rule, value, callback) => {
    //     if (!value) {
    //       return callback(new Error('账号不能为空'));
    //     }
    //     setTimeout(() => {
    //       if (!Number.isInteger(value)) {
    //         callback(new Error('请输入数字值'));
    //       } else {
    //         if (value < 18) {
    //           callback(new Error('必须年满18岁'));
    //         } else {
    //           callback();
    //         }
    //       }
    //     }, 1000);
    //   };
      var validatePass = (rule, value, callback) => {
        if (value === '') {
          callback(new Error('请输入密码'));
        } else {
          if (this.ruleForm.checkPass !== '') {
            this.$refs.ruleFormRef.validateField('checkPass');
          }
          callback();
        }
      };
      var validatePass2 = (rule, value, callback) => {
        if (value === '') {
          callback(new Error('请再次输入密码'));
        } else if (value !== this.ruleForm.password) {
          callback(new Error('两次输入密码不一致!'));
        } else {
          callback();
        }
      };
      return {
        ruleForm: {
          password: '',
          checkPass: '',
          username: ''
        },
        rules: {
          password: [
            { validator: validatePass, trigger: 'blur' }
          ],
          checkPass: [
            { validator: validatePass2, trigger: 'blur' }
          ],
          username: [
            // { validator: checkAge, trigger: 'blur' }
            { required: true, message: '请输入登录名称', trigger: 'blur' },
            { min: 3, max: 10, message: '长度在 3 到 10 个字符', trigger: 'blur' }
          ]
        }
      };
    },
    methods: {
      submitForm() {
        console.log(this,"thissssss");
        this.$refs.ruleFormRef.validate(async (valid) => {
           console.log(valid,"预验证");
          if (valid) {
           
            // alert('submit!');
            console.log(this.ruleForm.username,this.ruleForm.password,"dddd");
            let res = await login({username:this.ruleForm.username,password:this.ruleForm.password})
            
                console.log(res,"llll");
                console.log(res.data.data);
                if(res.data.meta.status === 200){
                    sessionStorage.setItem("token",res.data.data.token)
                    this.$message({
                        message: res.data.meta.msg,
                        type: 'success'
                        });
                    setTimeout(()=>{
                        this.$router.push("/")
                    },2000)
                    
                }else{
                   this.$message({
                        message: "用户名或密码错误",
                        type: 'error'
                        });
                }
            
          } else {
            console.log('error submit!!');
            return false;
          }
        });
      },
      resetForm() {
        this.$refs.ruleFormRef.resetFields();
      }
    },
    // destroyed(){
    //   clearTimeout(logtime)
    // }
  }
</script>

<style lang="less" scoped>
    .conment{
        width: 100vw;
        height: 100vh;
        position: absolute;
        left: 0;
        right: 0;
        z-index: 2;
        background: cornflowerblue;
        display: flex;
        justify-content: center;
        align-items: center;
        .el-form{
            padding: 20px;
            background: rgba(256,256,256,0.6);
            border-radius: 20px;
            
        }
    }
</style>